package com.avecsys.quotemanager.mbeans.menu;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.icefaces.ace.model.table.RowStateMap;

import com.avecsys.quotemanager.constants.BusinessCodes;
import com.avecsys.quotemanager.mbeans.core.BaseMBean;
import com.avecsys.quotemanager.pojos.UserMasterPojo;
import com.avecsys.quotemanager.pojos.menu.RootMenuEntity;
import com.avecsys.quotemanager.pojos.menu.SubMenuEntity;
import com.avecsys.quotemanager.service.menu.MenuManagerService;
import com.avecsys.quotemanager.utils.MessageBoardUtil;
import com.avecsys.quotemanager.utils.SystemConfig;
import com.icesoft.faces.component.menubar.MenuItem;
import com.icesoft.faces.context.effects.JavascriptContext;

public class MenuBarMBean extends BaseMBean
{

	private MenuManagerService menuManagerService;
	
	private  List<MenuItem> menuModel = new ArrayList<MenuItem>();

	/**
	 * @return the menuManagerService
	 */
	public MenuManagerService getMenuManagerService()
	{
		return menuManagerService;
	}

	/**
	 * @param menuManagerService the menuManagerService to set
	 */
	public void setMenuManagerService(MenuManagerService menuManagerService)
	{
		this.menuManagerService = menuManagerService;
		initilizeMenu();
	}

	/**
	 * 
	 * @return List<MenuItem>
	 */
	public List<MenuItem> getMenuModel() {
		return menuModel;
	}

	/**
	 * 
	 * @param menuModel
	 */
	public void setMenuModel(List<MenuItem> menuModel) {
		this.menuModel = menuModel;
	}

	public MenuBarMBean()
	{
		super();
	}

	public void actionSelectedMenuItem(ActionEvent actionEvent)
	{
		MessageBoardUtil.resetMessageBoard();
		MenuItem menuItem = (MenuItem) actionEvent.getSource();
		menuItem.setStyleClass("samClass");
		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), "updateFrame('"
				+ menuItem.getLink()
					+ "');");
	}

	public void initilizeMenu()
	{
		List<RootMenuEntity> rootMenuEntityList = menuManagerService.getAllMenus();
		buildMenuBar(rootMenuEntityList);
	}

	/**
	 * Method to build all the menu system from the database
	 * 
	 * @param rootMenuEntityList
	 */
	public void buildMenuBar(List<RootMenuEntity> rootMenuEntityList)
	{
		String contextPath = SystemConfig.getProperty("pagecontextpath");
		getMenuModel().clear();
		UserMasterPojo userMasterPojo = getSignedInUser();
		if (userMasterPojo != null)
		{
			String userType = userMasterPojo.getUserType();
			int indexRoot = 0;
			int indexSubRoot = 0;
			for (RootMenuEntity rootMenuEntity : rootMenuEntityList)
			{
				indexRoot++;
				MenuItem menuItem = new MenuItem();
				menuItem.setId("menuItem" + indexRoot);
				menuItem.setStyleClass("rootMenu");
				menuItem.setValue(getMenuName(rootMenuEntity.getRootMenuName()));
				
				List<SubMenuEntity> subMenusList = rootMenuEntity.getSubMenuEntityList();
				for (SubMenuEntity subMenuEntity : subMenusList)
				{
					if (subMenuEntity != null)
					{
						indexSubRoot++;
						MenuItem subMenuItem = new MenuItem();
						subMenuItem.setId("menuItem" + indexSubRoot);
						subMenuItem.setLink(subMenuEntity.getLinkPath());
						subMenuItem.setValue(getMenuName(subMenuEntity.getSubMenuName()));
						subMenuItem.setLink(contextPath + subMenuEntity.getLinkPath());
						subMenuItem.setStyleClass("subMenuItem");
						subMenuItem.setTarget("contentFrame");
						if (subMenuEntity.getIcon() != null
								&& subMenuEntity.getIcon().trim().length() > 0)
						{
							subMenuItem.setIcon(subMenuEntity.getIcon());
						}
						menuItem.getChildren().add(subMenuItem);
					}
				}

				if (BusinessCodes.USER_TYPE_SYSTEM_ADMIN.equals(userType))
				{
					// Menus I can see as system Admin
					if (BusinessCodes.MENU_TYPE_SYSTEM_ADMIN.equals(rootMenuEntity.getRootMenuRole()))
					{
						getMenuModel().add(menuItem);
					}
				}
				else if (BusinessCodes.USER_TYPE_SUPPLIER.equals(userType))
				{
					// Menus I can see as Supplier
					if (BusinessCodes.MENU_TYPE_SUPPLIER.equals(rootMenuEntity.getRootMenuRole()))
					{
						getMenuModel().add(menuItem);
					}
				}
				else if (BusinessCodes.USER_TYPE_BUYER.equals(userType))
				{
					// Menus I can see as Buyer
					if (BusinessCodes.MENU_TYPE_BUYER.equals(rootMenuEntity.getRootMenuRole()))
					{
						getMenuModel().add(menuItem);
					}
				}
			}
		}
		else
		{
			actionLogout(null);
		}
	}
}
